import React, { Suspense, useEffect } from "react"
import { useRoutes, useLocation, useNavigate } from "react-router-dom"
import routes from "@/router/index"
import Loading from "./components/Loading/Loading"
import { message } from "antd"

function App() {
  const location = useLocation()
  const navigate = useNavigate()

  // 全局拦截，如果没登录跳去登录页
  useEffect(() => {
    const isLogin = localStorage.getItem("isLogin")
    if (!isLogin && location.pathname !== "/login") {
      message.error("身份验证失败，请重新登陆")
      navigate("/login")
    }
  }, [location.pathname])

  return <Suspense fallback={<Loading></Loading>}>{useRoutes(routes)}</Suspense>
}

export default App
